/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*   File:      AGV_Display.cpp
*   Author:    刘洋兵
*   Function:  对数据库各表显示以及相关功能按键
*   Time:      2025-09-09
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "AGV_Display.h"
#include "ui_AGV_Display.h"

AGV_Display::AGV_Display(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AGV_Display)
{
    ui->setupUi(this);

    connect(this, &AGV_Display::sendlabellist, this, &AGV_Display::setlabeldata);
    connect(ui->tableView_vehicle_profile, &QTableView::clicked, this, &AGV_Display::getselecteddata);
    connect(ui->tableView_vehicle_status, &QTableView::clicked, this, &AGV_Display::getselecteddata);
    connect(ui->tableView, &QTableView::clicked, this, &AGV_Display::getselecteddata);

    connect(ui->tableView_map_config, &QTableView::clicked, this, &AGV_Display::getselecteddata);
    connect(ui->tableView_district_seats, &QTableView::clicked, this, &AGV_Display::getselecteddata);
    connect(ui->tableView_nodes, &QTableView::clicked, this, &AGV_Display::getselecteddata);
    connect(ui->tableView_paths, &QTableView::clicked, this, &AGV_Display::getselecteddata);

    connect(ui->tableView_forward_obstacle_strategy, &QTableView::clicked, this, &AGV_Display::getselecteddata);
    connect(ui->tableView_back_obstacle_strategy, &QTableView::clicked, this, &AGV_Display::getselecteddata);
    connect(ui->tableView_left_obstacle_strategy, &QTableView::clicked, this, &AGV_Display::getselecteddata);
    connect(ui->tableView_right_obstacle_strategy, &QTableView::clicked, this, &AGV_Display::getselecteddata);

    connect(ui->tableView_tasks, &QTableView::clicked, this, &AGV_Display::getselecteddata);

    connect(ui->tableView_vehicle_action_set, &QTableView::clicked, this, &AGV_Display::getselecteddata);
    connect(ui->tableView_action_group, &QTableView::clicked, this, &AGV_Display::getselecteddata);

    // 初始化滑动按键
    InitSwitchButton();

    // 初始化显示表数据
    InitTable();
    // 初始化下拉框数据
    InitComboBox();
}

AGV_Display::~AGV_Display()
{
    delete ui;
}

/**
 * @brief  初始化界面时将数据库各表读取显示在对应界面上
 */
void AGV_Display::InitTable()
{
    //openDB();
    on_pushButton_get_vehicle_fault_clicked();
    on_pushButton_get_vehicle_profile_clicked();
    on_pushButton_get_action_group_clicked();
    on_pushButton_get_vehicle_status_clicked();
    on_pushButton_get_forward_obstacle_strategy_clicked();
    on_pushButton_get_back_obstacle_strategy_clicked();
    on_pushButton_get_left_obstacle_strategy_clicked();
    on_pushButton_get_right_obstacle_strategy_clicked();
    on_pushButton_get_tasks_clicked();
    on_pushButton_get_map_config_clicked();
    on_pushButton_get_district_seats_clicked();
    on_pushButton_get_nodes_clicked();
    on_pushButton_get_paths_clicked();
    on_pushButton_get_vehicle_action_set_clicked();
}

/**
 * @brief  初始化下拉框显示数据
 */
void AGV_Display::InitComboBox()
{
    ui->comboBox->addItem("默认",-1);
    ui->comboBox->addItem("使用中",1);
    ui->comboBox->addItem("未使用",0);

    ui->comboBox_vehicle_type->addItem("默认",-1);
    ui->comboBox_vehicle_type->addItem("顶升车",1);
    ui->comboBox_vehicle_type->addItem("搬运车",2);
    ui->comboBox_vehicle_type->addItem("堆高车",3);

    ui->comboBox_is_parent_task->addItem("默认",-1);
    ui->comboBox_is_parent_task->addItem("源任务",1);
    ui->comboBox_is_parent_task->addItem("子任务",0);

    ui->comboBox_task_status->addItem("默认",-1);
    ui->comboBox_task_status->addItem("未执行",0);
    ui->comboBox_task_status->addItem("已分配",1);
    ui->comboBox_task_status->addItem("执行中",2);
    ui->comboBox_task_status->addItem("完成",3);
    ui->comboBox_task_status->addItem("人工辅助完成",4);
    ui->comboBox_task_status->addItem("已取消",5);
    ui->comboBox_task_status->addItem("暂停中",6);
    ui->comboBox_task_status->addItem("其他异常",7);
    ui->comboBox_task_status->addItem("失败",8);

    // 设置提示文字
    ui->lineEdit->setPlaceholderText("请输入任务ID");
    ui->lineEdit->setAlignment(Qt::AlignHCenter);
}

/**
 * @brief  初始化滑动按键
 */
void AGV_Display::InitSwitchButton()
{
    switchBtn_task = new SwitchButton(this);
    switchBtn_vehicle = new SwitchButton(this);
    switchBtn_task->setSize(70,30);
    switchBtn_task->setChecked(true);
    switchBtn_vehicle->setSize(70,30);
    switchBtn_vehicle->setChecked(true);
    connect(switchBtn_task, &SwitchButton::toggled, [&](bool checked) {
        if(checked)
        {
            MyDatabase::getInstance().startTimer("tb_tasks");
            MyDatabase::getInstance().startTimer("tb_tasks_error");
        }else {
            MyDatabase::getInstance().stopTimer("tb_tasks");
            MyDatabase::getInstance().stopTimer("tb_tasks_error");
        }
    });

    connect(switchBtn_vehicle, &SwitchButton::toggled, [&](bool checked) {
        if(checked)
        {
            MyDatabase::getInstance().startTimer("tb_vehicle_status");
            MyDatabase::getInstance().startTimer("tb_vehicle_status_error");
        }else {
            MyDatabase::getInstance().stopTimer("tb_vehicle_status");
            MyDatabase::getInstance().stopTimer("tb_vehicle_status_error");
        }
    });


    ui->trsk_refresh->addWidget(switchBtn_task, 0, Qt::AlignLeft);
    ui->trsk_refresh->setContentsMargins(0,0,0,0);
    ui->vehicle_status->addWidget(switchBtn_vehicle, 0, Qt::AlignLeft);
    ui->vehicle_status->setContentsMargins(0,0,0,0);
}

/**
 * @brief  显示指定窗口
 * @param  stackedWidgetindex  控件stackedWidget窗口索引
 * @param  tabWidgetindex  控件tabWidget窗口索引
 */
void AGV_Display::display_specified_window(const int& stackedWidgetindex, const int& tabWidgetindex)
{
    ui->stackedWidget->setCurrentIndex(stackedWidgetindex);
    switch (stackedWidgetindex)
    {
    case 0: ui->tabWidget_vehicle_management->setCurrentIndex(tabWidgetindex);break;
    case 1: ui->tabWidget_vehicle_fault->setCurrentIndex(tabWidgetindex);break;
    case 2: ui->tabWidget_map_config->setCurrentIndex(tabWidgetindex);break;
    case 3: ui->tabWidget_obstacle_avoidance->setCurrentIndex(tabWidgetindex);break;
    case 4: ui->tabWidget_task->setCurrentIndex(tabWidgetindex);break;
    case 5: ui->tabWidget_action->setCurrentIndex(tabWidgetindex);break;
    }
}

/**
 * @brief  返回鼠标选中的Table表格中某行的数据
 * @param  index  控件tabWidget单元格索引
 */
void AGV_Display::getselecteddata(const QModelIndex &index)
{
    QStringList datalist;
    QWidget* focused = QApplication::focusWidget();
    while(focused && !qobject_cast<QTableView*>(focused)) {
        focused = focused->parentWidget();
    }
    QTableView *table = qobject_cast<QTableView*>(focused);
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(table->model());

    for(int col=0; col<model->columnCount()-1; ++col){
        datalist<<model->data(model->index(index.row(), col)).toString();
    }


    emit sendlabellist(datalist, table);
}

/**
 * @brief  获取鼠标选中的Table表格中某行的数据，显示在窗口中
 * @param  listdata  需要显示的数据列表
 * @param  table  控件tabWidget对象指针
 */
void AGV_Display::setlabeldata(const QStringList& listdata, QTableView* table)
{
    if(table == ui->tableView_vehicle_profile)
    {
        ui->label__vehicle_name_profile->setText(listdata[1]);
        ui->label_brand_profile->setText(listdata[2]);
        ui->label_ip_profile->setText(listdata[3]);
        ui->label_port_profile->setText(listdata[4]);
        ui->label_vehicle_type_profile->setText(listdata[5]);
        ui->label_battery_type_profile->setText(listdata[6]);
        ui->label_charge_max_current_profile->setText(listdata[7]);
        ui->label_charge_max_voltage_profile->setText(listdata[8]);
        ui->label_is_available->setText(listdata[9]);
    }
    else if(table == ui->tableView_vehicle_status)
    {
        ui->label_vehicle_name_status->setText(listdata[1]);
        ui->label_vehicle_type_status->setText(listdata[2]);
        ui->label_vehicle_power->setText(listdata[3]);
        ui->label_auto_charge->setText(listdata[4]);
        ui->label_path_arrive_status->setText(listdata[5]);
        ui->label_action_complete_status->setText(listdata[6]);
        ui->label_vehicle_release->setText(listdata[7]);
        ui->label_is_suspended->setText(listdata[8]);
    }
    else if(table == ui->tableView)
    {
        ui->label_fault_code->setText(listdata[0]);
        ui->label_fault_info->setText(listdata[1]);
        ui->label_fault_category->setText(listdata[2]);
        ui->label_fault_level->setText(listdata[3]);
    } 

    else if(table == ui->tableView_map_config)
    {
        ui->label_map_name->setText(listdata[1]);
        ui->label_map_type->setText(listdata[2]);
        ui->label_workshop_name->setText(listdata[3]);
        ui->label_map_url->setText(listdata[4]);
        ui->label_X->setText(listdata[5]);
        ui->label_Y->setText(listdata[6]);
        ui->label_Z->setText(listdata[7]);
        ui->label_map_scale->setText(listdata[8]);
        ui->label_map_status->setText(listdata[9]);
    }
    else if(table == ui->tableView_district_seats)
    {
        ui->label_map_name_seats->setText(listdata[1]);
        ui->label_district_name->setText(listdata[2]);
        ui->label_district_type->setText(listdata[3]);
        ui->label_seat->setText(listdata[4]);
    }
    else if(table == ui->tableView_nodes)
    {
        ui->label_map_id_nodes->setText(listdata[1]);
        ui->label_node_name_nodes->setText(listdata[2]);
        ui->label_node_type_nodes->setText(listdata[3]);
        ui->label_X_nodes->setText(listdata[4]);
        ui->label_Y_nodes->setText(listdata[5]);
        ui->label_Z_nodes->setText(listdata[6]);
        ui->label_angle_nodes->setText(listdata[7]);
        ui->label_relocate_pos_nodes->setText(listdata[8]);
    }
    else if(table == ui->tableView_paths)
    {
        ui->label_map_anme_paths->setText(listdata[1]);
        ui->label_path_name_paths->setText(listdata[2]);
        ui->label_start_name->setText(listdata[3]);
        ui->label_goal_name->setText(listdata[4]);
        ui->label_X1->setText(listdata[5]);
        ui->label_Y1->setText(listdata[6]);
        ui->label_X2->setText(listdata[7]);
        ui->label_Y2->setText(listdata[8]);
        ui->label_direction->setText(listdata[9]);
        ui->label_forward_path_angle->setText(listdata[10]);
        ui->label_reverse_path_angle->setText(listdata[11]);
        ui->label_path_length->setText(listdata[12]);
        ui->label_path_width->setText(listdata[13]);
    }

    else if(table == ui->tableView_forward_obstacle_strategy)
    {
        ui->label_map_name_forward->setText(listdata[1]);
        ui->label_path_name_forward->setText(listdata[2]);
        ui->label_start_name_forward->setText(listdata[3]);
        ui->label_goal_name_forward->setText(listdata[4]);
        ui->label_camera_range_forward->setText(listdata[5]);
        ui->label_camera_switch_forward->setText(listdata[6]);
        ui->label_laser_range_forward->setText(listdata[7]);
        ui->label_laser_switch_forward->setText(listdata[8]);
        ui->label_nav_back_range_forward->setText(listdata[9]);
        ui->label_nav_back_switch_forward->setText(listdata[10]);
        ui->label_nav_laser_range_forward->setText(listdata[11]);
        ui->label_nav_laser_switch_forward->setText(listdata[12]);
        ui->label_path_speed_forward->setText(listdata[13]);
    }
    else if(table == ui->tableView_back_obstacle_strategy)
    {
        ui->label_map_name_back->setText(listdata[1]);
        ui->label_path_name_back->setText(listdata[2]);
        ui->label_start_name_back->setText(listdata[3]);
        ui->label_goal_name_back->setText(listdata[4]);
        ui->label_camera_range_back->setText(listdata[5]);
        ui->label_camera_switch_back->setText(listdata[6]);
        ui->label_fork_obstacle_switch_back->setText(listdata[7]);
        ui->label_laser_range_back->setText(listdata[8]);
        ui->label_laser_switch_back->setText(listdata[9]);
        ui->label_left_fork_laser_range_back->setText(listdata[10]);
        ui->label_left_fork_laser_switch_back->setText(listdata[11]);
        ui->label_right_fork_laser_range_back->setText(listdata[12]);
        ui->label_right_fork_laser_switch_back->setText(listdata[13]);
        ui->label_path_speed_back->setText(listdata[14]);
    }
    else if(table == ui->tableView_left_obstacle_strategy)
    {
        ui->label_map_name_left->setText(listdata[1]);
        ui->label_path_name_left->setText(listdata[2]);
        ui->label_start_name_left->setText(listdata[3]);
        ui->label_goal_name_left->setText(listdata[4]);
        ui->label_camera_range_left->setText(listdata[5]);
        ui->label_camera_switch_left->setText(listdata[6]);
        ui->label_laser_range_left->setText(listdata[7]);
        ui->label_laser_switch_left->setText(listdata[8]);
        ui->label_nav_back_range_left->setText(listdata[9]);
        ui->label_nav_back_switch_left->setText(listdata[10]);
        ui->label_path_speed_left->setText(listdata[11]);
    }
    else if(table == ui->tableView_right_obstacle_strategy)
    {
        ui->label_map_name_right->setText(listdata[1]);
        ui->label_path_name_right->setText(listdata[2]);
        ui->label_start_name_right->setText(listdata[3]);
        ui->label_goal_name_right->setText(listdata[4]);
        ui->label_camera_range_right->setText(listdata[5]);
        ui->label_camera_switch_right->setText(listdata[6]);
        ui->label_laser_range_right->setText(listdata[7]);
        ui->label_laser_switch_right->setText(listdata[8]);
        ui->label_nav_back_range_right->setText(listdata[9]);
        ui->label_nav_back_switch_right->setText(listdata[10]);
        ui->label_path_speed_right->setText(listdata[11]);
    }

    else if(table == ui->tableView_tasks)
    {
        ui->label_source_task->setText(listdata[1]);
        ui->label_creator->setText(listdata[2]);
        ui->label_task_name->setText(listdata[3]);
        ui->label_task_type->setText(listdata[4]);
        ui->label_transfer_trolley_id->setText(listdata[5]);
        ui->label_map_name_task->setText(listdata[6]);
        ui->label_vehicle_name_task->setText(listdata[7]);
        ui->label_priority->setText(listdata[8]);
        ui->label_start_node->setText(listdata[9]);
        ui->label_goal_node->setText(listdata[10]);
        ui->label_action_group->setText(listdata[11]);
        ui->label_task_status->setText(listdata[12]);
        ui->label_path_plan_task->setText(listdata[13]);
    }
    else if(table == ui->tableView_vehicle_action_set)
    {
        ui->label_action_name->setText(listdata[1]);
        ui->label_action_type->setText(listdata[2]);
        ui->label_action_height->setText(listdata[3]);
        ui->label_action->setText(listdata[4]);
        ui->label_immediate_status->setText(listdata[5]);
        ui->label_qr_code_id->setText(listdata[6]);
        ui->label_rotate_angle->setText(listdata[7]);
        ui->label_goods_angle->setText(listdata[8]);
        ui->label_drop_status->setText(listdata[9]);
        ui->label_shelves_name->setText(listdata[10]);
        ui->label_function_code->setText(listdata[11]);
        ui->label_exit_pose->setText(listdata[12]);
        ui->label_speed->setText(listdata[13]);
    }
    else if(table == ui->tableView_action_group)
    {
        ui->label_action_group_name->setText(listdata[1]);
        ui->label_start_action_code->setText(listdata[2]);
        ui->label_end_action_code->setText(listdata[3]);
    }
}

/*
 ***************** 获取数据库各表的数据 *****************
 */

//获取AGV故障码配置表数据
void AGV_Display::on_pushButton_get_vehicle_fault_clicked()
{
    QString sqlstr = "select fault_code,fault_info,fault_category,fault_level from tb_vehicle_fault";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"故障码","告警信息描述","故障类别","告警级别","操作"};
    tableData.HeaderList = header;
    tableData.table = ui->tableView;
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Edit};
    tableData.IconNum = iconlist;
    MyDatabase::getInstance().addTableMonitor("tb_vehicle_fault",tableData);
    ui->tableView->setColumnHidden(0,false);

}

//获取AGV车辆信息表数据
void AGV_Display::on_pushButton_get_vehicle_profile_clicked()
{
    QString sqlstr = "select vehicle_id,vehicle_name,brand,ip_address,port,vehicle_type,battery_type,charge_max_current,charge_max_voltage,is_available"
                     " from tb_vehicle_profile";
    // QTableView

    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","车辆名称","AGV品牌","车辆ip","车辆端口","AGV类型",
                         "电池类型","最大充电电流","最大充电电压","是否可用","操作"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"车辆名称",CellType::OTHER},
                                   {"AGV品牌",CellType::STRING_DATA},{"车辆ip",CellType::OTHER},
                                   {"车辆端口",CellType::OTHER},{"AGV类型",CellType::STRING_DATA},
                                   {"电池类型",CellType::STRING_DATA},{"最大充电电流",CellType::OTHER},
                                   {"最大充电电压",CellType::OTHER},{"是否可用",CellType::ICON},{"操作",CellType::OTHER}};


    tableData.HeaderList = header;
    tableData.table = ui->tableView_vehicle_profile;
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Pause,Icon_Edit,Icon_Positioning};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_vehicle_profile",tableData);

}

//获取小车状态
void AGV_Display::on_pushButton_get_vehicle_status_clicked()
{
    QString sqlstr = "select vehicle_status_id,vehicle_name,tb_vehicle_status.vehicle_type,vehicle_power,auto_charge,"
                     "path_arrive_status,action_complete_status,vehicle_release,is_suspended"
                     " from tb_vehicle_status"
                     " left join tb_vehicle_profile on tb_vehicle_status.vehicle_id = tb_vehicle_profile.vehicle_id";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","车辆名称","AGV类型","电量信息","充电状态","路径到达状态",
                         "动作完成状态","执行路径状态","运行状态","详细信息"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"车辆名称",CellType::OTHER},
                                   {"AGV类型",CellType::STRING_DATA},{"电量信息",CellType::OTHER},
                                   {"充电状态",CellType::STRING_DATA},{"路径到达状态",CellType::STRING_DATA},
                                   {"动作完成状态",CellType::STRING_DATA},{"执行路径状态",CellType::STRING_DATA},
                                   {"运行状态",CellType::STRING_DATA},{"详细信息",CellType::OTHER}};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_vehicle_status;
    QList<IconNumber> iconlist = {Icon_Detail};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_vehicle_status",tableData);

    // 每隔1秒刷新一次小车状态表
    MyDatabase::getInstance().startTimer("tb_vehicle_status");

    // 故障AGV显示
    vehicleFailed();
}

//车辆异常显示
void AGV_Display::vehicleFailed()
{
    QString sqlstr = "select vehicle_status_id,vehicle_name,tb_vehicle_status.vehicle_type,is_suspended,alarm_info"
                     " from tb_vehicle_status"
                     " left join tb_vehicle_profile on tb_vehicle_status.vehicle_id = tb_vehicle_profile.vehicle_id"
                     " WHERE alarm_info IS NOT NULL AND alarm_info <> ''";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","车辆名称","AGV类型","运行状态","故障信息","操作"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"车辆名称",CellType::OTHER},
                                   {"AGV类型",CellType::STRING_DATA}, {"故障信息",CellType::OTHER},
                                   {"操作",CellType::OTHER}};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_vehicle_status_error;
    QList<IconNumber> iconlist = {Icon_Manual};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_vehicle_status_error",tableData);

    // 每隔1秒刷新一次小车状态异常表
    MyDatabase::getInstance().startTimer("tb_vehicle_status_error");

}

//获取地图信息数据
void AGV_Display::on_pushButton_get_map_config_clicked()
{
    QString sqlstr = "select map_code,map_name,map_type,workshop_name,map_url,origin_x,origin_y,origin_z,map_scale,map_status"
                     " from tb_map_config";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","地图名称","地图类型","车间名称","地图文件位置URL","地图原点X值",
                         "地图原点Y值","地图原点Z值","地图比例","地图状态","操作"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"地图名称",CellType::OTHER},
                                   {"地图类型",CellType::STRING_DATA},{"车间名称",CellType::OTHER},
                                   {"地图文件位置URL",CellType::OTHER},{"地图原点X值",CellType::OTHER},
                                   {"地图原点Y值",CellType::OTHER},{"地图原点Z值",CellType::OTHER},
                                   {"地图比例",CellType::OTHER},{"地图状态",CellType::STRING_DATA},{"操作",CellType::OTHER}};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_map_config;
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Edit,Icon_Refresh};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_map_config",tableData);
}

//AGV 获取地图站点信息
void AGV_Display::on_pushButton_get_map_from_vehicle_clicked()
{
    emit sendtoAPIdisplay(nullptr, 1, 308, 0);
}

//AGV 获取当前地图pgm文件
void AGV_Display::on_pushButton_get_map_pgm_clicked()
{
    emit sendtoAPIdisplay(nullptr, 1 , 311, 0);
}

//AGV 获取当前地图原点坐标
void AGV_Display::on_pushButton_get_map_origin_clicked()
{
    emit sendtoAPIdisplay(nullptr, 1 , 312, 0);
}

//AGV 更新三方地图站点信息
void AGV_Display::on_pushButton_update_map_nodes_clicked()
{
    emit sendtoAPIdisplay(nullptr, 1 , 316, 0);
}

//AGV 查询设置的三方地图站点信息
void AGV_Display::on_pushButton_select_map_nodes_clicked()
{
    emit sendtoAPIdisplay(nullptr, 1 , 317, 0);
}

//获取区域站点表数据
void AGV_Display::on_pushButton_get_district_seats_clicked()
{
    QString sqlstr = "select district_seat_id,map_name,district_name,district_type,seat"
                     " from tb_district_seats"
                     " left join tb_map_config on tb_district_seats.map_code = tb_map_config.map_code";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","地图名称","区域名称","区域类型","座位号","操作"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"地图名称",CellType::OTHER},
                                   {"区域名称",CellType::OTHER},{"区域类型",CellType::STRING_DATA},
                                   {"座位号",CellType::OTHER},{"操作",CellType::OTHER}};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_district_seats;
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Edit};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_district_seats",tableData);
}

//获取车辆站点表数据
void AGV_Display::on_pushButton_get_nodes_clicked()
{
    QString sqlstr = "select node_id,map_name,node_name,node_type,node_x,node_y,node_z,angle,relocate_pos"
                     " from tb_nodes"
                     " left join tb_map_config on tb_nodes.map_code = tb_map_config.map_code";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","地图名称","站点名称","站点类型","X坐标值",
                         "Y坐标值","Z坐标值","重定位点角度","是否为重定位点","操作"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"地图名称",CellType::OTHER},
                                   {"站点名称",CellType::OTHER},{"站点类型",CellType::STRING_DATA},
                                   {"X坐标值",CellType::OTHER},{"Y坐标值",CellType::OTHER},
                                   {"Z坐标值",CellType::OTHER},{"重定位点角度",CellType::OTHER},
                                   {"是否为重定位点",CellType::STRING_DATA},{"操作",CellType::OTHER}};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_nodes;
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Edit};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_nodes",tableData);
}

//获取AGV路径信息表数据
void AGV_Display::on_pushButton_get_paths_clicked()
{
    QString sqlstr = "select path_id,map_name,path_name,start_name,goal_name,control_1_x,control_1_y,control_2_x,control_2_y,"
                     "direction,forward_path_angle,reverse_path_angle,path_length,path_width"
                     " from tb_paths"
                     " left join tb_map_config on tb_paths.map_code = tb_map_config.map_code";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","地图名称","路径名称","路径起点","路径终点","控制点X1坐标","控制点Y1坐标",
                         "控制点X2坐标","控制点Y2坐标","路径方向","路径方向(弧度)","双向路径反向方向","路径长度","路径宽度","操作"};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_paths;
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Edit};
    tableData.IconNum = iconlist;
    MyDatabase::getInstance().addTableMonitor("tb_paths",tableData);
}

//获取前方避障策略表数据
void AGV_Display::on_pushButton_get_forward_obstacle_strategy_clicked()
{
    QString sqlstr = "select obstacle_id,map_name,path_name,tb_node_A.node_name,tb_node_B.node_name,camera_range,camera_switch,laser_range,laser_switch,"
                     "nav_back_range,nav_back_switch,nav_laser_range,nav_laser_switch,path_speed"
                     " from tb_forward_obstacle_strategy"
                     " left join tb_nodes tb_node_A on tb_forward_obstacle_strategy.start_name = tb_node_A.node_id"
                     " left join tb_nodes tb_node_B on tb_forward_obstacle_strategy.goal_name = tb_node_B.node_id"
                     " left join tb_map_config on tb_forward_obstacle_strategy.map_code = tb_map_config.map_code";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","地图名称","路径名称","路径起点","路径终点","相机避障区域ID","避障相机开关",
                          "激光避障区域ID","避障激光开关","后导航激光避障区域ID","后导航避障激光开关",
                          "导航激光避障区域ID","导航避障激光开关","路径速度","操作"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"地图名称",CellType::OTHER},
                                   {"路径名称",CellType::OTHER},{"路径起点",CellType::OTHER},
                                   {"路径终点",CellType::OTHER},{"相机避障区域ID",CellType::OTHER},
                                   {"避障相机开关",CellType::STRING_DATA},{"激光避障区域ID",CellType::OTHER},
                                   {"避障激光开关",CellType::STRING_DATA},{"后导航激光避障区域ID",CellType::OTHER},
                                   {"后导航避障激光开关",CellType::STRING_DATA},{"导航激光避障区域ID",CellType::OTHER},
                                   {"导航避障激光开关",CellType::STRING_DATA},{"路径速度",CellType::OTHER},{"操作",CellType::OTHER}};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_forward_obstacle_strategy;
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Edit};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_forward_obstacle_strategy",tableData);
}

//获取后方避障策略表数据
void AGV_Display::on_pushButton_get_back_obstacle_strategy_clicked()
{
    QString sqlstr = "select obstacle_id,map_name,path_name,tb_node_A.node_name,tb_node_B.node_name,camera_range,camera_switch,fork_obstacle_switch,laser_range,laser_switch,"
                     "left_fork_laser_range,left_fork_laser_switch,right_fork_laser_range,right_fork_laser_switch,path_speed"
                     " from tb_back_obstacle_strategy"
                     " left join tb_nodes tb_node_A on tb_back_obstacle_strategy.start_name = tb_node_A.node_id"
                     " left join tb_nodes tb_node_B on tb_back_obstacle_strategy.goal_name = tb_node_B.node_id"
                     " left join tb_map_config on tb_back_obstacle_strategy.map_code = tb_map_config.map_code";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","地图名称","路径名称","路径起点","路径终点","相机避障区域ID","避障相机开关","叉尖点激光开关",
                          "激光避障区域ID","避障激光开关","左叉尖激光避障区域ID","左叉尖避障激光开关",
                          "右叉尖激光避障区域ID","右叉尖避障激光开关","路径速度","操作"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"地图名称",CellType::OTHER},
                                   {"路径名称",CellType::OTHER},{"路径起点",CellType::OTHER},
                                   {"路径终点",CellType::OTHER},{"相机避障区域ID",CellType::OTHER},
                                   {"避障相机开关",CellType::STRING_DATA},{"叉尖点激光开关",CellType::STRING_DATA},
                                   {"激光避障区域ID",CellType::OTHER},{"避障激光开关",CellType::STRING_DATA},
                                   {"左叉尖激光避障区域ID",CellType::OTHER},{"左叉尖避障激光开关",CellType::STRING_DATA},
                                   {"右叉尖激光避障区域ID",CellType::OTHER},{"右叉尖避障激光开关",CellType::STRING_DATA},
                                   {"路径速度",CellType::OTHER},{"操作",CellType::OTHER}};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_back_obstacle_strategy;
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Edit};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_back_obstacle_strategy",tableData);
}

//获取左方避障策略表数据
void AGV_Display::on_pushButton_get_left_obstacle_strategy_clicked()
{
    QString sqlstr = "select obstacle_id,map_name,path_name,tb_node_A.node_name,tb_node_B.node_name,camera_range,camera_switch,laser_range,laser_switch,"
                     "middle_laser_range,middle_laser_switch,path_speed"
                     " from tb_left_obstacle_strategy"
                     " left join tb_nodes tb_node_A on tb_left_obstacle_strategy.start_name = tb_node_A.node_id"
                     " left join tb_nodes tb_node_B on tb_left_obstacle_strategy.goal_name = tb_node_B.node_id"
                     " left join tb_map_config on tb_left_obstacle_strategy.map_code = tb_map_config.map_code";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","地图名称","路径名称","路径起点","路径终点","相机避障区域ID","避障相机开关",
                          "激光避障区域ID","避障激光开关","中部激光避障区域ID","中部避障激光开关",
                          "路径速度","操作"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"地图名称",CellType::OTHER},
                                   {"路径名称",CellType::OTHER},{"路径起点",CellType::OTHER},
                                   {"路径终点",CellType::OTHER},{"相机避障区域ID",CellType::OTHER},
                                   {"避障相机开关",CellType::STRING_DATA},{"激光避障区域ID",CellType::OTHER},
                                   {"避障激光开关",CellType::STRING_DATA},
                                   {"中部激光避障区域ID",CellType::OTHER},{"中部避障激光开关",CellType::STRING_DATA},
                                   {"路径速度",CellType::OTHER},{"操作",CellType::OTHER}};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_left_obstacle_strategy;
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Edit};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_left_obstacle_strategy",tableData);
}

//获取右方避障策略表数据
void AGV_Display::on_pushButton_get_right_obstacle_strategy_clicked()
{
    QString sqlstr = "select obstacle_id,map_name,path_name,tb_node_A.node_name,tb_node_B.node_name,camera_range,camera_switch,laser_range,laser_switch,"
                     "middle_laser_range,middle_laser_switch,path_speed"
                     " from tb_right_obstacle_strategy"
                     " left join tb_nodes tb_node_A on tb_right_obstacle_strategy.start_name = tb_node_A.node_id"
                     " left join tb_nodes tb_node_B on tb_right_obstacle_strategy.goal_name = tb_node_B.node_id"
                     " left join tb_map_config on tb_right_obstacle_strategy.map_code = tb_map_config.map_code";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","地图名称","路径名称","路径起点","路径终点","相机避障区域ID","避障相机开关",
                          "激光避障区域ID","避障激光开关","中部激光避障区域ID","中部避障激光开关",
                          "路径速度","操作"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"地图名称",CellType::OTHER},
                                   {"路径名称",CellType::OTHER},{"路径起点",CellType::OTHER},
                                   {"路径终点",CellType::OTHER},{"相机避障区域ID",CellType::OTHER},
                                   {"避障相机开关",CellType::STRING_DATA},{"激光避障区域ID",CellType::OTHER},
                                   {"避障激光开关",CellType::STRING_DATA},
                                   {"中部激光避障区域ID",CellType::OTHER},{"中部避障激光开关",CellType::STRING_DATA},
                                   {"路径速度",CellType::OTHER},{"操作",CellType::OTHER}};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_right_obstacle_strategy;
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Edit};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_right_obstacle_strategy",tableData);
}

//获取任务表数据
void AGV_Display::on_pushButton_get_tasks_clicked()
{
    QString sqlstr = "select task_id,source_task_id,creator,task_name,task_type,transfer_trolley_id,map_name,vehicle_name,"
                     "priority,tb_node_A.node_name,tb_node_B.node_name,action_group_name,task_status,path_plan"
                     " from tb_tasks"
                     " left join tb_map_config on tb_tasks.map_code = tb_map_config.map_code"
                     " left join tb_vehicle_profile on tb_tasks.vehicle_id = tb_vehicle_profile.vehicle_id"
                     " left join tb_nodes tb_node_A on tb_tasks.start_node_id = tb_node_A.node_id"
                     " left join tb_nodes tb_node_B on tb_tasks.goal_node_id = tb_node_B.node_id"
                     " left join tb_action_group on tb_tasks.action_group_id = tb_action_group.action_group_id";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","第三方任务","任务创建者","任务名称","任务类型","笼车ID","地图名称",
                          "车辆名称","任务优先级","任务起点","任务终点","动作组ID",
                          "任务执行状态","任务规划路径","操作"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"第三方任务",CellType::OTHER},
                                   {"任务创建者",CellType::OTHER},{"任务名称",CellType::OTHER},
                                   {"任务类型",CellType::STRING_DATA},{"笼车ID",CellType::OTHER},
                                   {"地图名称",CellType::OTHER},{"车辆名称",CellType::OTHER},
                                   {"任务优先级",CellType::OTHER},{"任务起点",CellType::OTHER},
                                   {"任务终点",CellType::OTHER},{"动作组ID",CellType::OTHER},
                                   {"任务执行状态",CellType::STRING_DATA},{"任务规划路径",CellType::OTHER},{"操作",CellType::OTHER}};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_tasks;
    QList<IconNumber> iconlist = {Icon_Cancel,Icon_Edit};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_tasks",tableData);
    ui->tableView_tasks->setColumnHidden(0,false);
    // 每隔1秒刷新一次任务表
    MyDatabase::getInstance().startTimer("tb_tasks");

    // 异常任务显示
    taskFailed();
}

// 任务异常显示
void AGV_Display::taskFailed()
{
    QString sqlstr = "select task_id,source_task_id,task_name,task_type,vehicle_name,"
                     "tb_node_A.node_name,tb_node_B.node_name,task_failed_msg"
                     " from tb_tasks"
                     " left join tb_vehicle_profile on tb_tasks.vehicle_id = tb_vehicle_profile.vehicle_id"
                     " left join tb_nodes tb_node_A on tb_tasks.start_node_id = tb_node_A.node_id"
                     " left join tb_nodes tb_node_B on tb_tasks.goal_node_id = tb_node_B.node_id"
                     " where task_status = 7 OR task_status = 8 ";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","第三方任务","任务名称","任务类型","车辆名称","任务起点","任务终点","任务异常原因","操作"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"第三方任务",CellType::OTHER},{"任务名称",CellType::OTHER},
                                   {"任务类型",CellType::STRING_DATA},{"车辆名称",CellType::OTHER},
                                   {"任务起点",CellType::OTHER},{"任务终点",CellType::OTHER},
                                   {"任务异常原因",CellType::OTHER},{"操作",CellType::OTHER}};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_tasks_error;
    QList<IconNumber> iconlist = {Icon_Replace_Task,Icon_Replace,Icon_Manual};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_tasks_error",tableData);
    ui->tableView_tasks_error->setColumnHidden(0,false);
    // 每隔1秒刷新一次任务表
    MyDatabase::getInstance().startTimer("tb_tasks_error");
}

//获取动作指令表数据
void AGV_Display::on_pushButton_get_vehicle_action_set_clicked()
{
    QString sqlstr = "select action_code,action_name,action_type,action_height,action,immediate_status,"
                     "qr_code_id,rotate_angle,goods_angle,drop_status,shelves_name,function_code,exit_pose,speed"
                     " from tb_vehicle_action_set";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","动作名称","动作类型","推高车高度","动作","是否边走边动作","需校验二维码ID",
                          "车载旋转角度","货物旋转角度","安全检查状态","货架名称","功能码",
                          "顶升车货架识别退出点","退出点的运行速度","操作"};
    QMap<QString,CellType> cell = {{"id",CellType::OTHER},{"动作名称",CellType::OTHER},
                                   {"动作类型",CellType::STRING_DATA},{"推高车高度",CellType::OTHER},
                                   {"动作",CellType::STRING_DATA},{"是否边走边动作",CellType::STRING_DATA},
                                   {"需校验二维码ID",CellType::OTHER},{"车载旋转角度",CellType::OTHER},
                                   {"货物旋转角度",CellType::OTHER},{"安全检查状态",CellType::OTHER},
                                   {"货架名称",CellType::OTHER},{"功能码",CellType::OTHER},
                                   {"顶升车货架识别退出点",CellType::OTHER},{"退出点的运行速度",CellType::OTHER},{"操作",CellType::OTHER}};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_vehicle_action_set;
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Edit};
    tableData.IconNum = iconlist;
    tableData.cellDataFormat = cell;
    tableData.is_true = false;
    MyDatabase::getInstance().addTableMonitor("tb_vehicle_action_set",tableData);

}

//获取动作组表数据
void AGV_Display::on_pushButton_get_action_group_clicked()
{
    QString sqlstr = "select action_group_id,action_group_name,action_set_A.action_name,action_set_B.action_name"
                     " from tb_action_group"
                     " left join tb_vehicle_action_set action_set_A on tb_action_group.start_action_code = action_set_A.action_code"
                     " left join tb_vehicle_action_set action_set_B on tb_action_group.end_action_code = action_set_B.action_code";
    // QTableView
    TableViewData tableData;
    tableData.sql = sqlstr;
    QStringList header = {"id","动作组名称","任务开始动作","任务结束动作","操作"};
    tableData.HeaderList = header;
    tableData.table = ui->tableView_action_group;
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Edit};
    tableData.IconNum = iconlist;
    MyDatabase::getInstance().addTableMonitor("tb_action_group",tableData);
}

/*
 ***************** 界面添加数据到各表 *****************
 */
void AGV_Display::addvehicle(const QStringList& list)
{
    QList<IconNumber> iconlist = {Icon_Delete,Icon_Pause,Icon_Edit,Icon_Positioning};
    emit sendnewAGV(ui->tableView_vehicle_profile, list, iconlist);
}

void AGV_Display::on_pushButton_add_clicked()
{
    emit sendtablename(ui->tableView, 0, 1);
}

void AGV_Display::on_pushButton_add_map_config_clicked()
{
    emit sendtablename(ui->tableView_map_config, 0, 1);
}

void AGV_Display::on_pushButton_add_district_seats_clicked()
{
    emit sendtablename(ui->tableView_district_seats, 0, 1);
}

void AGV_Display::on_pushButton_add_forward_obstacle_strategy_clicked()
{
    emit sendtablename(ui->tableView_forward_obstacle_strategy, 0, 1);
}

void AGV_Display::on_pushButton_add_back_obstacle_strategy_clicked()
{
    emit sendtablename(ui->tableView_back_obstacle_strategy, 0, 1);
}

void AGV_Display::on_pushButton_add_left_obstacle_strategy_clicked()
{
    emit sendtablename(ui->tableView_left_obstacle_strategy, 0, 1);

}

void AGV_Display::on_pushButton_add_right_obstacle_strategy_clicked()
{
    emit sendtablename(ui->tableView_right_obstacle_strategy, 0, 1);
}

void AGV_Display::on_pushButton_add_tasks_clicked()
{
    emit sendtablename(ui->tableView_tasks, 0, 1);
}

void AGV_Display::on_pushButton_add_vehicle_action_set_clicked()
{
    emit sendtablename(ui->tableView_vehicle_action_set, 0, 1);
}

void AGV_Display::on_pushButton_clicked()
{
    emit sendtablename(ui->tableView_action_group, 0, 1);
}

void AGV_Display::on_pushButton_add_nodes_clicked()
{
    emit sendtablename(ui->tableView_nodes, 0, 1);
}

void AGV_Display::on_pushButton_add_paths_clicked()
{
    emit sendtablename(ui->tableView_paths, 0, 1);
}

/*
 ***************** 赛选数据显示 *****************
 */

/**
 * @brief  设置赛选tableView界面数据的显示
 * @param  comboBox 下拉框对象指针
 * @param  tablenem 表名
 * @param  sql 数据库操作语句
 */
void AGV_Display::ShowFilterData(QComboBox *comboBox,
                    const QString &tablenem,
                    const QString &sql)
{
    if(!comboBox)
        return;
    if(tablenem.isEmpty() || sql.isEmpty())
        return;

    int filter = (comboBox->currentText() == "默认") ?
                     -1 : comboBox->currentData().toInt();
    QMap<int, bool> filterData;
    QString sqlstr = sql;
    QList<QStringList> datalist = MyDatabase::getInstance().query_table(sqlstr);
    for (int row = 0; row < datalist.count(); row++) {
        bool is_filter = false;
        int typeNumber = datalist[row][0].toInt();
        if(filter == typeNumber || filter==-1)
        {
            is_filter = true;
        }
        filterData[row] = is_filter;
    }
    MyDatabase::getInstance().setProxy(filterData,tablenem);
}


void AGV_Display::on_comboBox_currentTextChanged(const QString &arg1)
{
    if(arg1.isEmpty())
    {
        return;
    }
    QString sqlstr = QString("select is_available,created_time from tb_vehicle_profile");
    ShowFilterData(ui->comboBox, "tb_vehicle_profile", sqlstr);
}


void AGV_Display::on_comboBox_vehicle_type_currentTextChanged(const QString &arg1)
{
    if(arg1.isEmpty())
    {
        return;
    }
    QString sqlstr = QString("select vehicle_type,created_time from tb_vehicle_profile");
    ShowFilterData(ui->comboBox_vehicle_type, "tb_vehicle_profile", sqlstr);
}

void AGV_Display::on_comboBox_is_parent_task_currentTextChanged(const QString &arg1)
{
    if(arg1.isEmpty())
    {
        return;
    }
    QString sqlstr = QString("select is_parent_task,create_time from tb_tasks");
    ShowFilterData(ui->comboBox_is_parent_task, "tb_tasks", sqlstr);
}

void AGV_Display::on_comboBox_task_status_currentTextChanged(const QString &arg1)
{
    if(arg1.isEmpty())
    {
        return;
    }

    QString sqlstr = QString("select task_status,create_time from tb_tasks");
    ShowFilterData(ui->comboBox_task_status, "tb_tasks", sqlstr);
}

void AGV_Display::on_pushButton_search_clicked()
{
    if(ui->lineEdit->text().isEmpty())
    {
        return;
    }
    QString id = ui->lineEdit->text();
    QMap<int, bool> filterData;
    QString sqlstr = QString("select task_id,create_time from tb_tasks");
    QList<QStringList> datalist = MyDatabase::getInstance().query_table(sqlstr);
    for (int row = 0; row < datalist.count(); row++) {
        bool is_filter = false;
        if(id == datalist[row][0])
        {
            is_filter = true;
        }
        filterData[row] = is_filter;
    }
    MyDatabase::getInstance().setProxy(filterData,"tb_tasks");
}
